﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Gapper
{
    class Player : Gapper.Sprite2D
    {
        private Point direction = new Point(1, 0);
        private GraphicsDevice graphics;
        private Texture2D pathMap;
        private Texture2D tripMap;


        public Player(GraphicsDevice graphics)
        {
            this.graphics = graphics;
        }

        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager contentManager, string assetName)
        {
            pathMap = contentManager.Load<Texture2D>("level2path");
            base.LoadContent(contentManager, assetName);
        }
        public override void Update()
        {
            //Check for direction and change the position
            KeyboardState keystate = Keyboard.GetState();
            if (keystate.IsKeyDown(Keys.Up)) { direction.Y = -1; direction.X = 0; }
            else if (keystate.IsKeyDown(Keys.Down)) { direction.Y = 1; direction.X = 0; }
            else if (keystate.IsKeyDown(Keys.Left)) { direction.Y = 0; direction.X = -1; }
            else if (keystate.IsKeyDown(Keys.Right)) { direction.Y = 0; direction.X = 1; }
            else if (keystate.IsKeyDown(Keys.Space)) { direction.Y = 0; direction.X = 0; }

            Color[] pathColor = new Color[(int)(Math.Abs(direction.X + direction.Y) * 2)];
            Rectangle r = new Rectangle((int)Position.X, (int)Position.Y, ((int)direction.X * 2>0?(int)direction.X * 2:1), ((int)direction.Y * 2>0)?(int)direction.Y * 2:1);
            pathMap.GetData<Color>(0, r, pathColor, 0, pathColor.Length);

            //Check to see if the path exists in the direction the player needs to move
            int indexToGet = ((int)(direction.X + direction.Y) * 2 < 0) ? 0 : (int)(direction.X + direction.Y) * 2;
            if (pathColor[indexToGet-1].PackedValue != 0)
            {
                Position.X += direction.X;
                Position.Y += direction.Y;

                if (Position.X < 0) Position.X = 0;
                if (Position.X + texture.Width > graphics.Viewport.TitleSafeArea.Right) Position.X = graphics.Viewport.TitleSafeArea.Right - texture.Width;
                if (Position.Y < 0) Position.Y = 0;
                if (Position.Y + texture.Height > graphics.Viewport.TitleSafeArea.Bottom) Position.Y = graphics.Viewport.TitleSafeArea.Bottom - texture.Height;

                if (direction.X == 1) RotationAngle = 360;
                if (direction.X == -1) RotationAngle = 180;
                if (direction.Y == 1) RotationAngle = 90;
                if (direction.Y == -1) RotationAngle = 270;
            }

            base.Update();
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Vector2 v = new Vector2(17, 11);

            spriteBatch.Draw(pathMap, v, Color.White);
            //spriteBatch.Draw(tripMap, v, Color.White);
            base.Draw(spriteBatch);
        }
    }
}
